package midone.exchangeskill.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 圆形的效果 可当做下载进度条
 * Created by NekoNeko on 16/11/26.
 */

public class ProgressCircle extends TextView {
    int degrees = 0;
    int startDegrees = -90;
    int circleWith = 5;
    Paint paint;

    /**
     * 更新弧度
     *
     * @param degrees
     */
    public void updateDegrees(int degrees) {
        this.degrees = degrees;
        postInvalidate();
    }

    public ProgressCircle(Context context) {
        super(context);
    }

    public ProgressCircle(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.parseColor("#B7B7B7"));
        paint.setStrokeWidth(circleWith);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public ProgressCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);
    }

    private int getLength() {
        return getWidth() > getHeight() ? getHeight() : getWidth();

    }

    private void drawCircle(Canvas canvas) {
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, getLength() / 2 - circleWith / 2, paint);
        drawProcessArc(canvas, startDegrees, degrees);

    }

    private void drawProcessArc(Canvas canvas, int startDegrees, int degrees) {
        int left = getWidth() / 2 - getLength() / 2;
        int top = getHeight() / 2 - getLength() / 2;
        int right = getWidth() / 2 + getLength() / 2;
        int bottom = getHeight() / 2 + getLength() / 2;
        RectF rectF = new RectF(left, top, right, bottom);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawArc(rectF, startDegrees, degrees, true, paint);
    }
}
